﻿using ERPSystem.Common.Domains.Notices;
using ERPSystem.Common.Domains.ReturnNotice;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using MediatR;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using SalesManagementCommon.API.SalesManagementCommons.ReturnNotices;

namespace SalesManagementCommon.API.SalesManagementCommons.BillingInvoicing
{
    public class BillingInvoicingExcelHander : IRequestHandler<BillingInvoicingExcelCommand, FileExportResult>
    {
        private readonly IBaseRepository<Invoicingmemo> invoicingmemoRepository;

        public BillingInvoicingExcelHander(IBaseRepository<Invoicingmemo> invoicingmemoRepository)
        {
            this.invoicingmemoRepository = invoicingmemoRepository;
        }

        /// <summary>
        /// 开票结算单导出
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<FileExportResult> Handle(BillingInvoicingExcelCommand request, CancellationToken cancellationToken)
        {
            var result = new FileExportResult();
            var list = await (await invoicingmemoRepository.GetAll()).ToListAsync();
            Console.WriteLine(list.Count);
            IWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet();
            IRow row1 = sheet.CreateRow(0);

            row1.CreateCell(0).SetCellValue("序号");
            row1.CreateCell(1).SetCellValue("单据编码");
            row1.CreateCell(2).SetCellValue("单据日期");
            row1.CreateCell(3).SetCellValue("单据状态");
            row1.CreateCell(4).SetCellValue("业务状态");
            row1.CreateCell(5).SetCellValue("业务类型");
            row1.CreateCell(6).SetCellValue("客户名称");
            row1.CreateCell(7).SetCellValue("蓝字红字");
            row1.CreateCell(8).SetCellValue("税率");
            row1.CreateCell(9).SetCellValue("发票类型");
            row1.CreateCell(10).SetCellValue("开户行");
            row1.CreateCell(11).SetCellValue("账号");
            row1.CreateCell(12).SetCellValue("税号");
            row1.CreateCell(13).SetCellValue("地址电话");
            row1.CreateCell(14).SetCellValue("交付邮箱");
            row1.CreateCell(15).SetCellValue("备注信息");



            int rowIndex = 1;
            foreach (var item in list)
            {
                row1 = sheet.CreateRow(rowIndex);
                row1.CreateCell(0).SetCellValue(item.InvoicingmemoId);
                row1.CreateCell(1).SetCellValue(item.Documentsnumber);
                row1.CreateCell(2).SetCellValue(item.DocumentsDate);
                row1.CreateCell(3).SetCellValue(item.Documentstatus);
                row1.CreateCell(4).SetCellValue(item.BusinessstateId);
                row1.CreateCell(5).SetCellValue(item.BusinessTypeId);
                row1.CreateCell(6).SetCellValue(item.ClientName);
                row1.CreateCell(7).SetCellValue(item.BlueandredId);
                row1.CreateCell(8).SetCellValue(item.RateId);
                row1.CreateCell(9).SetCellValue(item.InvoiceTypeId);
                row1.CreateCell(10).SetCellValue(item.Bank);
                row1.CreateCell(11).SetCellValue(item.Account);
                row1.CreateCell(12).SetCellValue(item.RateNumber);
                row1.CreateCell(13).SetCellValue(item.Addresstelephone);
                row1.CreateCell(14).SetCellValue(item.Deliverymailbox);
                row1.CreateCell(15).SetCellValue(item.BeiZ);

                rowIndex++;

            }
            byte[] data = null;
            using (MemoryStream ms = new MemoryStream())
            {
                book.Write(ms);
                data = ms.ToArray();
            }
            string contextType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            string fileName = "开票通知单.xlsx";

            result.FileBytes = data;
            result.MimeType = contextType;
            result.FileName = fileName;
            return result;
        }
    }
}
